<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <meta charset="UTF-8">
		<title>添加学生申请</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
	    <meta name="description" content="Creative - Bootstrap 3 Responsive Admin Template">
	    <meta name="author" content="GeeksLabs">
	    <meta name="keyword" content="Creative, Dashboard, Admin, Template, Theme, Bootstrap, Responsive, Retina, Minimal">
	    <link rel="shortcut icon" href="img/favicon.png">
  <body>
		<c:import url="admin_home.jsp"></c:import>
     	
     	<!--main content start-->
     	<div class="layui-body container" style="margin-top: 100px">
     		<div class="layui-row" >
     				<form class="layui-form" action="#">
     					<div class="layui-form-item">
					      <div class="layui-input-inline layui-col-md-offset2 layui-col-md4">
					     	<input type="text" id="stuName" name="stuName" lay-verify="required" placeholder="输入学生姓名" autocomplete="off" class="layui-input">
					      </div>
					      <div class="layui-col-md4">
					      	<button class="btn btn-primary" type="button" id="select_btn">查询</button>
					  		</div>
					  	</div>
					 </form>
     		</div>
     	<!-- 显示表格数据 -->
     		<div class="layui-row">
	     		<div class="layui-col-md-offset10 layui-col-md1">
	     			<button id="addStu" class="layui-btn layui-btn-lg">录入学生信息</button>
	     		</div>
     			<div class="layui-col-md-offset1 layui-col-md10">
     				<table class="table table-hover" id="applyTable">
     					<thead>
     					<tr>
     						<th>ID</th>
     						<th>团队名称</th>
     						<th>姓名</th>
     						<th>学号</th>
     						<th>性别</th>
     						<th>班级</th>
     						<th>邮箱</th>
     						<th>联系方式</th>
     						<th>操作</th>
     					</tr>
     					</thead>
     					<tbody id="stuInfo_tbody">
     					
     					</tbody>
     					</table>
     			</div>
     		</div>
     		<div class="layui-row">
     			<div class="layui-col-md-offset1 layui-col-md3" id="page_info_area">
     				
     			</div>
     			<div class="col-md-5" id="page_info_nav">
     			</div>
     		</div>
     	</div>
      	<!--main content end-->
      	
		<script>
		$("#select_btn").click(function(){
			var stuName=$("#stuName").val();
			$("#stuInfo_tbody").empty();
			$.ajax({
	    		url:"${pageContext.request.contextPath}/admin/stuApplyList.do",
	    		data:"stuName="+stuName,
	    		type:"GET",
	    		success:function(result){
	    			//console.log(result);
	    			//1.解析并显示数据
	    			
	    			build_tableInfo(result);
	    			//2.解析显示分页信息
	    			build_pageInfo(result);
	    			build_page_nav(result);
	    			
	    		}
	    	});
		});
		 $(function(){
		    	toPage(1);
		 });
		    function toPage(pn){
		    	$.ajax({
		    		url:"${pageContext.request.contextPath}/admin/stuApplyList.do",
		    		data:"pageNum="+pn,
		    		type:"GET",
		    		success:function(result){
		    			//console.log(result);
		    			//1.解析并显示数据
		    			build_tableInfo(result);
		    			//2.解析显示分页信息
		    			build_pageInfo(result);
		    			build_page_nav(result);
		    			
		    		}
		    	});
		    }
		    
		    //解析显示分页信息
		    function build_tableInfo(result){
		    	$("#stuInfo_tbody").empty();
		    	if(result.code=200){
		    		layer.msg('无申请信息!', {icon: 2,time:1000});
		    		return;
		    	}
		    	var applyInfo=result.extend.page.list;
		    	
		    	$.each(applyInfo,function(index,item){
		    		var id=$("<td></td>").append(item.id);
		    		var groupName=$("<td></td>").append(item.groupName);
		    		var stuName=$("<td></td>").append(item.student.stuName);
		    		var stuId=$("<td></td>").append(item.student.stuId);
		    		var stuSex=$("<td></td>").append(item.student.stuSex);
		    		var stuClass=$("<td></td>").append(item.student.stuClass);
		    		var teaEmail=$("<td></td>").append(item.student.stuEmail);
		    		var teaTell=$("<td></td>").append(item.student.stuTell);
		    		var stateText=$("<h3></h3>").append("<small></small>");
		    		var editBtn=$("<button></button>").addClass("btn btn-primary btn-sm success_btn").append("录入");
		    		var delBtn=$("<button></button>").addClass("btn btn-danger btn-sm fail_btn").append("驳回");
		    		var applyState=item.applyState;
		    		var btnTd=$("<td></td>");
		    		if(applyState==0){
		    			btnTd.append(editBtn).append("  ").append(delBtn);
		    		}else if(applyState==1){
		    			btnTd.append(stateText).append("已录入");
		    		}else if(applyState==2){
		    			btnTd.append(stateText).append("已驳回");
		    		}
		    		
		    		$("<tr></tr>").append(id).append(groupName).append(stuName).append(stuId)
		    					.append(stuSex).append(stuClass).append(teaEmail).append(teaTell).append(btnTd).appendTo("#applyTable tbody");
		    	});
		    }
		    //解析显示分页信息
		    var currentPage;
		    function build_pageInfo(result){
		    	if(result.code=200){
		    		return;
		    	}
		    	$("#page_info_area").empty();
		    	currentPage=result.extend.page.pageNum;
		    	$("#page_info_area").append("当前"+result.extend.page.pageNum+"页，共"+result.extend.page.pages+"页,一共"+result.extend.page.total+"条记录");
		    	totalRecord=result.extend.page.total;
		    }
		    //创建分页条
		    function build_page_nav(result){
		    	if(result.code=200){
		    		return;
		    	}
		    	$("#page_info_nav").empty();
		    	var ul=$("<ul></ul>").addClass("pagination");
		    	var firstPage=$("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
		    	var prePage=$("<li></li>").append($("<a></a>").append("上一页"));
				if(result.extend.page.hasPreviousPage==false){
					firstPage.addClass("disabled");  
					prePage.addClass("prePage");
		    	}
				firstPage.click(function(){
					toPage(1);
				});
				prePage.click(function(){
					if(result.extend.page.pageNum==1){
						toPage(1);
					}else{
						toPage(result.extend.page.pageNum-1);
					}
					
				});
		    	var nextPage=$("<li></li>").append($("<a></a>").append("下一页"));
		    	var lastPage=$("<li></li>").append($("<a></a>").append("尾页").attr("href","#"));
		    	if(result.extend.page.hasNextPage==false){
		    		nextPage.addClass("disabled");  
		    		lastPage.addClass("prePage");
		    	}
		    	lastPage.click(function(){
					toPage(result.extend.page.pages);
				});
		    	nextPage.click(function(){
		    		if(result.extend.page.pageNum==result.extend.page.pages){
		    			toPage(result.extend.page.pages);
		    		}else{
		    			toPage(result.extend.page.pageNum+1);
		    		}
					
				});
		    	ul.append(firstPage).append(prePage);
		    	$.each(result.extend.page.navigatepageNums,function(index,item){
		    		var numLi=$("<li></li>").append($("<a></a>").append(item));
		    		if(result.extend.page.pageNum==item){
		    			numLi.addClass("active");
		    		}
		    		numLi.click(function(){
		    			toPage(item);
		    		});
		    		ul.append(numLi);
		    	});
		    	ul.append(nextPage).append(lastPage);
		    	var navElv=$("<nv></nv>").append(ul);
		    	navElv.appendTo("#page_info_nav");
		    }
		    //申请驳回
		    $(document).on("click",".fail_btn",function(){
		    	var id=$(this).parents("tr").find("td:eq(0)").text();
		    	var stuName=$(this).parents("tr").find("td:eq(2)").text();
		    	if(confirm("确认驳回录入学生："+stuName+"的申请吗？")){
		    		$.ajax({
		    			url:"${pageContext.request.contextPath}/admin/updateState.do",
		    			data:{id:id,state:2},
		    			type:"GET",
		    			success:function(result){
		    				console.log("result:"+result);
		    				toPage(currentPage);
		    			}
		    		});
		    	}
		    });
		    
		  //同意录入
		    $(document).on("click",".success_btn",function(){
		    	var id=$(this).parents("tr").find("td:eq(0)").text();
		    	var stuName=$(this).parents("tr").find("td:eq(2)").text();
		    	if(confirm("确认录入学生："+stuName+"吗？")){
		    		$.ajax({
		    			url:"${pageContext.request.contextPath}/admin/updateState.do",
		    			data:{id:id,state:1},
		    			type:"GET",
		    			success:function(result){
		    				console.log("result:"+result);
		    				toPage(currentPage);
		    			}
		    		});
		    	}
		    });
	     
		  
		  layui.use('table', function(){
			  var table = layui.table;
	        });
	        
	      
		  //录入学生
	      $('#addStu').on('click',function(){
	      	layer.open({
		      type: 2,
		      title: '录入学生信息',
		      maxmin: true,
		      shadeClose: true, //点击遮罩关闭层
		      area : ['800px' , '600px'],
		      content: '${pageContext.request.contextPath}/admin/addStuInfo.do'
		      
		    });
	      });
	  </script>
  </body>
</html>
